﻿using System;
using System.Collections.Generic;
using Framework;
using TrueSync;


public enum EventTriggerTypeEnum
{
    TimeFrameTrigger,
    LoopFrameTrigger,
    CalbackTrigger,
}

public class Event
{
    public int ID;
    public EventTriggerTypeEnum mEventTriggerType;
    //TimeFrameTrigger
    public int mTriggerFrameNo;
    //LoopFrameTrigger
    public int mStartFrameNo;
    public int mIntervalFrameNum;

    public bool Enabled;
    BaseEventEffector mEventEffectors;

    int mFrameNum;
    ObjHandle<LogicEntity> mOwner;

    int DoEffectFrames;

    public void Init(ObjHandle<LogicEntity> owner)
    {
        mOwner = owner;

        if(mEventTriggerType == EventTriggerTypeEnum.CalbackTrigger)
        {
            mOwner.handle.GetLogicComponent<LogicEventComponent>().RegisterCallbackEvent(this);
        }

        mFrameNum = 0;
        DoEffectFrames = 0;
        Enabled = true;
    }

    public void Tick(FP deltaTime)
    {
        if (Enabled == false)
            return;

        if (DoEffectFrames > 0)
        {
            DoEffectFrames--;
            if (DoEffectFrames <= 0)
            {
                mEventEffectors.DoFinish(mOwner);
            }
            else
            {
                mEventEffectors.DoEvent(mOwner);
            }
        }

        mFrameNum++;
        if (mEventTriggerType == EventTriggerTypeEnum.TimeFrameTrigger)
        {
            if (mFrameNum >= mIntervalFrameNum)
            {
                DoEffect();
            }
        }
        else if (mEventTriggerType == EventTriggerTypeEnum.LoopFrameTrigger)
        {
            if (mFrameNum - mStartFrameNo > 0)
            {
                if ((mFrameNum - mStartFrameNo) % mIntervalFrameNum == 0)
                {
                    DoEffect();
                }
            }
        }
    }

    public void OnDestroy()
    {
        mEventEffectors.OnDestroy();
        mEventEffectors = null;
    }

    public void DoEffect(params object[] callbackParams)
    {
        if (mEventEffectors is JudgeEventEffector)
        {
            DoEffectFrames = ((JudgeEventEffector)mEventEffectors).DoEffectFrames;
        }
        mEventEffectors.DoStart(mOwner);
        mEventEffectors.DoEvent(mOwner, callbackParams);
        if (DoEffectFrames == 0)
            mEventEffectors.DoFinish(mOwner);
    }

    public void Serialize(EByte ms)
    {
        ms.WriteInt(ID);
        ms.WriteInt((int)mEventTriggerType);
        switch(mEventTriggerType)
        {
            case EventTriggerTypeEnum.TimeFrameTrigger:
                ms.WriteInt(mTriggerFrameNo);
                break;
            case EventTriggerTypeEnum.LoopFrameTrigger:
                ms.WriteInt(mStartFrameNo);
                ms.WriteInt(mIntervalFrameNum);
                break;
        }
        ms.WriteString(mEventEffectors.GetType().Name);
        mEventEffectors.Serialize(ms);
    }

    public void Deserialize(EByte ms)
    {
        ID = ms.ReadInt();
        mEventTriggerType = (EventTriggerTypeEnum)ms.ReadInt();
        switch(mEventTriggerType)
        {
            case EventTriggerTypeEnum.TimeFrameTrigger:
                mTriggerFrameNo = ms.ReadInt();
                break;
            case EventTriggerTypeEnum.LoopFrameTrigger:
                mStartFrameNo = ms.ReadInt();
                mIntervalFrameNum = ms.ReadInt();
                break;
        }
        mEventEffectors = EventEffectorFactory.Instance.GetEventEffectorByName(ms.ReadString());
        mEventEffectors.Deserialize(ms); 
    }
}



